package com.learn.config;

import jakarta.servlet.Filter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/02/11
 * @description 全注解开发，自定义配置类，相当于web.xml的配置
 */
// 表示是一个配置类, 继承后似乎可以不加
//@Configuration
public class MyConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 表示配置spring.xml文件
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    /**
     * 表示配置springmvc.xml文件
     * @return 返回springmvc的配置类
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MySpringmvcConfig.class};
    }

    /**
     * 配置DispatcherServlet的url<url-pattern>
     *
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 配置过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        // 可以setForceEncoding表示请求和响应都强制编码
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8", true);
        // 发送put和delete请求的
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
    }
}
